/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.client.data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import nu.zoom.android.gdata.project.client.WKC;
import android.content.Context;
import android.util.Log;
import android.widget.ArrayAdapter;

public class ProjectnamesStorage {
	private static final String LOG_TAG = WKC.LOG_TAG;

	public static void save(final Context context,
			final ArrayAdapter<String> adapter) throws PersistenceException {
		final FileOutputStream fileOutputStream;
		BufferedWriter writer = null;
		final int count = adapter.getCount();
		Log.d(LOG_TAG, "Save " + count + " project names") ;
		try {
			fileOutputStream = context.openFileOutput(WKC.PROJECTNAMES_FILE,
					Context.MODE_PRIVATE);
			writer = new BufferedWriter(new OutputStreamWriter(
					fileOutputStream, "utf-8"));
			for (int n = 0; n < count; n++) {
				writer.write(adapter.getItem(n));
				writer.newLine();
			}
		} catch (FileNotFoundException e) {
			Log.e(LOG_TAG, "Unable to open private file: "
					+ WKC.PROJECTNAMES_FILE, e);
			throw new PersistenceException(e);
		} catch (UnsupportedEncodingException e) {
			Log.e(LOG_TAG, "Unable to write private file: "
					+ WKC.PROJECTNAMES_FILE, e);
			throw new PersistenceException(e);
		} catch (IOException e) {
			Log.e(LOG_TAG, "Unable to write private file: "
					+ WKC.PROJECTNAMES_FILE, e);
			throw new PersistenceException(e);
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					Log.e(LOG_TAG,
							"Unable to close file stream when saving project names");
				}
			}
		}
	}

	public static ArrayAdapter<String> load(final Context context,
			final int resourceID) throws PersistenceException {
		final ArrayAdapter<String> result = new ArrayAdapter<String>(context,
				resourceID);
		BufferedReader reader = null;
		try {
			final FileInputStream fileInputStream = context
					.openFileInput(WKC.PROJECTNAMES_FILE);
			reader = new BufferedReader(new InputStreamReader(fileInputStream,
					"utf-8"));
			String line = reader.readLine();
			while (line != null) {
				result.add(line);
				line = reader.readLine();
			}
		} catch (FileNotFoundException e) {
			Log.e(LOG_TAG, "Unable to open private file: "
					+ WKC.PROJECTNAMES_FILE, e);
		} catch (UnsupportedEncodingException e) {
			Log.e(LOG_TAG, "Unable to read private file: "
					+ WKC.PROJECTNAMES_FILE, e);
			throw new PersistenceException(e);
		} catch (IOException e) {
			Log.e(LOG_TAG, "Unable to read private file: "
					+ WKC.PROJECTNAMES_FILE, e);
			throw new PersistenceException(e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					Log.e(LOG_TAG,
							"Unable to close file when reading project names");
				}
			}
		}
		Log.d(LOG_TAG, "Loaded " + result.getCount() + " project names into adapter.");
		return result;
	}
}
